﻿	<div class="easyui-layout" data-options="fit:true,border:false" style="overflow:hidden;padding:0;margin:0px auto">
		<div data-options="region:'center',border:false">
			  <table id="sfe_pm_add_dialog_datagrid"></table>
		</div>
	</div>
	<div id="pa_addplandialog_dialog_id"></div>
<script type="text/javascript">
var sfe_pm_add_dialog_datagrid,rowEditFlag = [],pmadd_upload_savedata=''; // 
var pm_addplandialog_date = formatCalendarDate(week_variable_date,'-');
$(function(){
     getDayVisiptplanShowData();
});

/**
* 计算本周第一天0时0刻,是否显示增删改查按钮
*/
function checkOperate(statusValue){
	var currentdate = new Date();
	currentdate = new Date(currentdate.setDate(currentdate.getDate()- currentdate.getDay()));
    var currentweekstartdate = new Date(currentdate.getFullYear(),currentdate.getMonth(),currentdate.getDate());
	if(week_variable_date.getTime() < currentweekstartdate.getTime() || statusValue == 40){
	     $('#sfe_mpadddialog_add_toolbar').linkbutton('disable');
	     $('#sfe_mpadddialog_update_toolbar').linkbutton('disable');
		 $('#sfe_mpadddialog_undoupdate_toolbar').linkbutton('disable');
		 $('#sfe_mpadddialog_remove_toolbar').linkbutton('disable');
		 $('#sfe_mpadddialog_save_toolbar').linkbutton('disable');
	}else{
		 $('#sfe_mpadddialog_add_toolbar').linkbutton('enable');
	     $('#sfe_mpadddialog_update_toolbar').linkbutton('enable');
		 $('#sfe_mpadddialog_undoupdate_toolbar').linkbutton('enable');
		 $('#sfe_mpadddialog_remove_toolbar').linkbutton('enable');
		 $('#sfe_mpadddialog_save_toolbar').linkbutton('enable');
	}
}

function getDayVisiptplanShowData() {
		var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		var value = new Array('wsActionJSON', token, 'p_sfeweekplanupdate', 'weekplandata',"{'currentdate':'" + pm_addplandialog_date +"','employeeid':" +oval_user_search+ "}");
		var dataStr = getJsonData(variable, value);
		connectionServlet(dataStr, getDayVisiptplanShowDataReturn);
	};

function getDayVisiptplanShowDataReturn(data) {
	var tempResult,rowsData,processselData,parentempselData,visitplanstatusData;
	if (data.successflag == 'false') {
		return;
	}
	tempResult = data.result;
	if (typeof(tempResult) == 'undefined') {
		return;
	}
	
	if (typeof(tempResult.allcustomer) != 'undefined') {
		rowsData = tempResult.allcustomer;
	} else {
		rowsData = [];
	}
	if (typeof(tempResult.processsel) != 'undefined') {
		processselData = tempResult.processsel;
	} else {
		processselData = [];
	}
	if (typeof(tempResult.parentempsel) != 'undefined') {
		parentempselData = tempResult.parentempsel;
		parentempselData.push({"id" : "0","text" : "---"});
	} else {
		parentempselData = [];
	}
	if (typeof(tempResult.visitplanstatus) != 'undefined') {
		visitplanstatusData = tempResult.visitplanstatus;
	} else {
		visitplanstatusData = {"status" : -1};
	}
	var daytpeSel = [{
			daytype : 'am',
			text : '上午'
		}, {
			daytype : 'pm',
			text : '下午'
		}, {
			daytype : 'all',
			text : '全天'
		}
	];
	var needassistSel = [{
			needassist : 'Y',
			text : '是'
		}, {
			needassist : 'N',
			text : '否'
		}
	];
	
	sfe_pm_add_dialog_datagrid = $('#sfe_pm_add_dialog_datagrid').datagrid({
			border : false,
			striped : true,
			nowrap : true,
			fitColumns : true,
			height : 300,
			idField : 'visitfeedbackid',
			fit : true,
			columns : [[{
						field : 'visitfeedbackid',
						title : '客户名称',
						width : 20,
						resizable : false,
						checkbox : true
					}, {
						field : 'orgname',
						title : '客户名称',
						width : 150,
						resizable : false
					}, {
						field : 'daytype',
						title : '上午/下午',
						width : 60,
						resizable : false,
						formatter : function (value, row, index) {
							if (value != null) {
								if (value == 'am') {
									return '上午';
								} else if (value == 'pm') {
									return '下午';
								} else {
									return '全天';
								}
							} else {
								return '';
							}
						},
						editor : {
							type : 'combobox',
							options : {
								valueField : 'daytype',
								textField : 'text',
								data : daytpeSel,
								required : true,
								panelHeight : 'auto'
							}
						}
					}, {
						field : 'processid',
						title : '拜访程序',
						width : 80,
						resizable : false,
						formatter : function (value, row, index) {
							if (value != null) {
								if (value == 0) {
									return '';
								} else {
									for (var i = 0; i < processselData.length; i++) {
										if (processselData[i].id == value)
											return processselData[i].text;
									}
								}
							} else {
								return '';
							}
						},
						editor : {
							type : 'combobox',
							options : {
								valueField : 'id',
								textField : 'text',
								data : processselData,
								required : true,
								panelHeight : 'auto'
							}
						}
						
					}, {
						field : 'needassist',
						title : '是否协访',
						width : 60,
						resizable : false,
						formatter : function (value, row, index) {
							if (value != null) {
								if (value == 'Y') {
									return '是';
								} else {
									return '否';
								}
							} else {
								return '';
							}
						},
						editor : {
							type : 'combobox',
							options : {
								valueField : 'needassist',
								textField : 'text',
								data : needassistSel,
								panelHeight : 'auto'
							}
						}
					}, {
						field : 'mainvisituserid',
						title : '协访主管',
						width : 60,
						resizable : false,
						formatter : function (value, row, index) {
							if (value != null) {
								if (value == 0) {
									return '';
								} else {
									for (var i = 0; i < parentempselData.length; i++) {
										if (parentempselData[i].id == value)
											return parentempselData[i].text;
									}
								}
							} else {
								return '';
							}
						},
						editor : {
							type : 'combobox',
							options : {
								valueField : 'id',
								textField : 'text',
								data : parentempselData,
								panelHeight : 'auto'
							}
						}
					}
				]],
			toolbar : [{
					id : 'sfe_mpadddialog_add_toolbar',
					text : '添加',
					iconCls : 'icon-add',
					handler : function () {
						if (rowEditFlag.length == 0) {
							PMAddPlanShowSummary();
						}
					}
				}, '-', {
					id : 'sfe_mpadddialog_update_toolbar',
					text : '修改',
					iconCls : 'icon-edit',
					handler : function () {
						
						editPlan();
					}
				}, '-', {
					id : 'sfe_mpadddialog_undoupdate_toolbar',
					text : '取消修改',
					iconCls : 'icon-undo',
					handler : function () {
						if (rowEditFlag.length > 0) {
							sfe_pm_add_dialog_datagrid.datagrid('rejectChanges');
							sfe_pm_add_dialog_datagrid.datagrid('unselectAll');
							sfe_pm_add_dialog_datagrid.datagrid('uncheckAll');
							rowEditFlag = [];
						}
					}
				}, '-', {
					id : 'sfe_mpadddialog_remove_toolbar',
					text : '删除',
					iconCls : 'icon-remove',
					handler : function () {
						if (rowEditFlag.length == 0) {
							var rows = sfe_pm_add_dialog_datagrid.datagrid('getChecked');
							if (rows.length > 1) {
								$.messager.confirm('确认', '您确定要删除已勾选的所有选项吗?', function (r) {
									if (r) {
										var ids = [];
										for (var i = 0; i < rows.length; i++) {
											ids.push(rows[i].visitfeedbackid);
										}
										removeVisiptplanData(ids.join(','));
									}
								});
								
							} else {
								$.messager.alert('提示', '请至少需要勾选一项！', 'info');
							}
						} else {
							$.messager.alert('提示', '您有正在编辑的项，不能删除!', 'info');
						}
					}
				}, '-', {
					id : 'sfe_mpadddialog_save_toolbar',
					text : '保存',
					iconCls : 'icon-save',
					handler : function () {
						if (rowEditFlag.length > 0) {
							for (var j = 0; j < rowEditFlag.length; j++) {
								if (j > 0) {
									pmadd_upload_savedata += ",";
								}
								sfe_pm_add_dialog_datagrid.datagrid('endEdit', rowEditFlag[j]);
							}
							rowEditFlag = [];
							sfe_pm_add_dialog_datagrid.datagrid('unselectAll');
							sfe_pm_add_dialog_datagrid.datagrid('uncheckAll');
							updateVisiptplanData(pmadd_upload_savedata);
						}
					}
				}, '-', {
					text : '刷新',
					iconCls : 'icon-refresh',
					handler : function () {
						sfe_pm_add_dialog_datagrid.datagrid('rejectChanges');
						sfe_pm_add_dialog_datagrid.datagrid('unselectAll');
						sfe_pm_add_dialog_datagrid.datagrid('uncheckAll');
						getDayVisiptplanShowData();
					}
				}
			],
			onAfterEdit : function (rowIndex, rowData, changes) {
				var mainvisituserid = 0,
				processid = 0,
				needassist = 'N';
				if (rowData.mainvisituserid != '') {
					mainvisituserid = rowData.mainvisituserid;
				}
				if (rowData.processid != '') {
					processid = rowData.processid;
				}
				if (rowData.needassist != '') {
					needassist = rowData.needassist;
				}
				pmadd_upload_savedata += "{'daytype':'" + rowData.daytype + "','visitfeedbackid':" + rowData.visitfeedbackid + ",'processid':" + processid + ",'mainvisituserid':" + mainvisituserid + ",'needassist':'" + needassist + "'}";
				
			},
			onUncheck : function (rowIndex, rowData) {
				if (rowEditFlag.length > 0) {
					sfe_pm_add_dialog_datagrid.datagrid('cancelEdit', rowIndex);
					for (var m = 0; m < rowEditFlag.length; m++) {
						if (rowEditFlag[m] == rowIndex) {
							rowEditFlag.splice(m, 1);
						}
					}
				}
				
			},
			onUnselect : function (rowIndex, rowData) {}
		});
	sfe_pm_add_dialog_datagrid.datagrid('loadData', rowsData);
	checkOperate(visitplanstatusData.status);
}

/**
* 更新修改的数据
*/
function updateVisiptplanData(newData) {
         pmadd_upload_savedata = '';
		 var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		 var value = new Array('wsActionJSON', token, 'p_sfeweekplanupdate', 'savenewdata',"{'newdatavector':["+ newData + "],'employeeid':" + oval_user_search+ "}");
		 var dataStr = getJsonData(variable, value);
		connectionServlet(dataStr, updateVisiptplanDataReturn);
	};

/**
* 编辑数据
*/
function editPlan(){
	  var  rows =  sfe_pm_add_dialog_datagrid.datagrid('getChecked');
	  if(rows.length > 0 ){
			 if(rowEditFlag.length == 0){
			     for(var i=0;i<rows.length;i++){
				     var index  = sfe_pm_add_dialog_datagrid.datagrid('getRowIndex',rows[i]);
				     sfe_pm_add_dialog_datagrid.datagrid('beginEdit',index);
			         rowEditFlag.push(index);
				 }
				
			 }else{
			    sfe_pm_add_dialog_datagrid.datagrid('unselectAll');
				sfe_pm_add_dialog_datagrid.datagrid('uncheckAll');
				for(var j=0;j<rowEditFlag.length;j++){
				     sfe_pm_add_dialog_datagrid.datagrid('endEdit',rowEditFlag[j]);
				}
				rowEditFlag = [];
			 }
	  }else{
			 $.messager.alert('提示','请选择需要修改的项','info' );
	  }

}


/**
* 删除数据
*/
function removeVisiptplanData(ids) {	
		 var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		 var value = new Array('wsActionJSON', token, 'p_sfeweekplanupdate', 'deleteplan',"{'ids':'" + ids + "'}");
		 var dataStr = getJsonData(variable, value);
		 connectionServlet(dataStr, removeVisiptplanDataReturn);
	};

function removeVisiptplanDataReturn(data){
     if (data.successflag == 'false') {
		return;
	}
    getDayVisiptplanShowData();
}



function updateVisiptplanDataReturn(data){
    if (data.successflag == 'false') {
		return;
	}
	$.messager.show({
		title:'提示',
		msg:'数据更新成功！',
		timeout:5000,
		showType:'slide'
   });
}

/**
* 弹出需要增加数据的summary
*/
function PMAddPlanShowSummary(){
     $('#sfe_pm_addplan_dialog').dialog('destroy');
     $('#pa_addplandialog_dialog_id').append('<div id="sfe_pm_addplan_dialog"></div>');
     $('#sfe_pm_addplan_dialog').dialog({
		 title: '添加计划',
         width: 600,   
         height: 400,
		 closed: false,
		 modal: true,
		 href:'PMAddPlanDialogSummary.html',
         buttons:[{
				text:'确定',
				handler:function(){
				    addSaveSummaryData();
				}
			},{
				text:'取消',
				handler:function(){
				     $('#sfe_pm_addplan_dialog').dialog('close');
					 $('#sfe_pm_addplan_dialog').dialog('destroy');
				}
			}]		
		 });
}

function addSaveSummaryData() {
         var  summary_rows =   $('#sfe_pm_add_dialogsummary_datagrid').datagrid('getChecked');
		 var params = '[';
		 for(var i=0;i<summary_rows.length;i++){
		      if(i>0){
			     params +=",";
			  }
			  params +="{'customerid':" + summary_rows[i].orgid +"}";
		 }
		 params +=']';
		 var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		 var value = new Array('wsActionJSON', token, 'p_sfeweekplanupdate', 'savesummarydata',"{'weeknum':"+weeknumInt+",'summarydata':"+ params+ ",'currentdate':'" + pm_addplandialog_date +"','employeeid':" + oval_user_search+ "}");
		 var dataStr = getJsonData(variable, value);
		connectionServlet(dataStr, addSaveSummaryDataReturn);
	};

function addSaveSummaryDataReturn(data){
     if (data.successflag == 'false') {
		return;
	}
	getDayVisiptplanShowData();
	$('#sfe_pm_addplan_dialog').dialog('close');
	$('#sfe_pm_addplan_dialog').dialog('destroy');
}
</script>
